package ipdesk.model.report.invade.dao;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.dao.client.DaoManager;


import ipdesk.common.Codes;
import ipdesk.model.report.invade.bean.InvadeInfoBean;
import ipdesk.model.report.seminar.bean.SeminarInfoBean;

import ecg.dao.DaoService;


public class InvadeInfoDao
{
	private static InvadeInfoDao singleton;
	
	protected InvadeInfoDao(){}
	
	public static InvadeInfoDao getInstance()
	{
		if(singleton == null)
		{
			singleton = new InvadeInfoDao();
		}
		
		return singleton;
	}
	
	private DaoManager	manager	= DaoService.getInstance();
	private IInvadeInfo invadeInfo 	= (IInvadeInfo)manager.getDao(IInvadeInfo.class, "ip_desk");
	
	/*
	 * 입력받은 데이터를 DB에 insert 하는 메서드.
	 */
	public int invadeInfoReg(InvadeInfoBean invadeInfoBean) throws Exception
	{
		int lastInvadeSeq = 0;
		try{
			// 트랜잭션 시작
			manager.startTransaction();
			
			// 상담정보 insert.
			lastInvadeSeq = invadeInfo.invadeInfoReg(invadeInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}

		return lastInvadeSeq;
	}
	
	public void insertFileData(InvadeInfoBean invadeInfoBean) throws Exception
	{
		try{
			manager.startTransaction();
			
			invadeInfo.insertFileData(invadeInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}						
		
	}
	
	public int getDataTotalCount(InvadeInfoBean invadeInfoBean) throws Exception
	{
		
		int totalDataCount = invadeInfo.getDataTotalCount(invadeInfoBean); 
		
		return totalDataCount;
	}
	
	public List<InvadeInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		
		List<InvadeInfoBean> pageDataList = invadeInfo.getPagingDataListForUser(map);
		
		/*
		for(int i=0; i < pageDataList.size(); i++)
		{
			// 코드값을 코드에 해당하는 스트링으로 변환.
			String iprCodeValue = Codes.getInstance().getFromIprCodeToValue(pageDataList.get(i).getIprCode());
			
			// 변환된 스트링을 다시 빈에 저장.
			pageDataList.get(i).setIprCode(iprCodeValue);

		}
		*/
		return pageDataList; 
	}
	
	public InvadeInfoBean getDetailData(int invadeSeq) throws Exception
	{
		InvadeInfoBean invadeInfoBean = invadeInfo.getDetailData(invadeSeq);
		
		invadeInfoBean.setDifficulty(invadeInfoBean.getDifficulty().replace("\n", "<br/>"));
		//invadeInfoBean.setSurveyContent(invadeInfoBean.getSurveyContent().replace("<br/>", "\n"));
		if(invadeInfoBean.getBustPlan()!=null){
			invadeInfoBean.setBustPlan(invadeInfoBean.getBustPlan().replace("\n", "<br/>"));
		}
		return invadeInfoBean;
	}
	
	public void invadeInfoUpdate(InvadeInfoBean invadeInfoBean) throws Exception
	{
		System.out.println("***수정메소드2 들어옴");
		try{
			manager.startTransaction();
			// 2. seminar DB update
			invadeInfo.invadeInfoUpdate(invadeInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
	}
	
	public void delFileData(String fileSeq) throws Exception
	{
		invadeInfo.delFileData(fileSeq);
	}
	
	public void delDetailData(int invadeSeq) throws Exception
	{
		invadeInfo.delDetailData(invadeSeq);
	}
	
	public void insertCompleteData(InvadeInfoBean invadeInfoBean) throws Exception 
	{
		invadeInfo.insertCompleteData(invadeInfoBean);
	}
	
	public InvadeInfoBean getFileDetailData(int invadeSeq, String fileCategory) throws Exception
	{
		InvadeInfoBean invadeInfoBean = new InvadeInfoBean();
		invadeInfoBean.setInvadeSeq(invadeSeq);
		invadeInfoBean.setFile_category(fileCategory);
		
		return invadeInfo.getFileDetailData(invadeInfoBean);
	}
	
	public void insertAdminData(InvadeInfoBean invadeInfoBean) throws Exception
	{
		invadeInfo.insertAdminData(invadeInfoBean);
	}
}
